﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace websitev2.Core
{
    public class App
    {

        public static Models.Info.AppDetails GetAppDetails(Int64 AppID)
        {
            Models.Info.AppDetails oReturn = new Models.Info.AppDetails();
            try
            {

                
                Models.StartStopEntities db = new Models.StartStopEntities();

                var app = db.WindowAppLists.FirstOrDefault(a => a.AppID == AppID);

                oReturn.FirstSeenOn = app.FirstSeenOn.GetValueOrDefault();


                oReturn.ClassifiedAsID = app.TypeID.GetValueOrDefault();

                var classification = db.WindowAppTypes.FirstOrDefault(a => a.TypeID == app.TypeID);
                oReturn.ClassifiedAsName = classification.TypeName;
                oReturn.Signature = app.Signature;
                oReturn.AppID = app.AppID;
                oReturn.Description = app.Description;


                oReturn.MoreInformationURL = app.MoreInformationURL;
                oReturn.LongDescription = app.LongDescription; 

                // Returns the total time in seconds. 
                string _totalTimeLogged = SqlAccessCore.ExecuteScalar("select SUM(ElapsedTimeSeconds) from UsersAppLog where AppID = " + AppID.ToString());
                oReturn.TotalTimeLogged = new TimeSpan(0, 0, int.Parse(_totalTimeLogged));


                TimeSpan _NumberOfDaysBetween = DateTime.Now - oReturn.FirstSeenOn;

                double _SecondsPerDay = oReturn.TotalTimeLogged.TotalSeconds / _NumberOfDaysBetween.TotalDays;
                int _Seconds = (int)_SecondsPerDay;

                oReturn.AveragePerDay = new TimeSpan(0, 0, _Seconds);
            }
            catch { } 

            // Next try to load the image
            string _imageURL = websitev2.Core.Image.CheckImageExists(oReturn.AppID, 101); 
            if (_imageURL!="")
            {
                _imageURL = "/uploads/appimages/" + _imageURL;
            }


            oReturn.ImageURL = _imageURL; 
            
            return oReturn; 
        }

    }
}